var token = localStorage.getItem('token');
var avator = localStorage.getItem('avator');
var nickname = localStorage.getItem('nickname');
var can_send_wishes = localStorage.getItem('can_send_wishes');
var api = "http://mytest.api.com/index.php/";
var domain = "http://mytest.api.com";
var isLingQuCdk;

$(function () {
    $("#myAvator").attr('src', domain + avator);
    $("#myNickName").html(nickname);
    if (can_send_wishes === '1') {
        $(".subWishLine").attr("style", "display:block;");//显示div
    } else {
        $(".subWishLine").attr("style", "display:none;");//隐藏div
    }

    //点赞数
    $.ajax({
        url: api + "index/getMyPraiseNum",
        data: {
            token: token,
        },
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data.status != 0) {
                alert(data.msg);
                return false;
            }
            $(".dzNum").html(data.data.dzNum);
            $(".receiveNum").html(data.data.reveiveNum);
            isLingQuCdk = data.data.isLingQu;
            if (data.data.isLingQu == 1) {
                $(".lingqu").html('cdk:' + data.data.CDK);
            }

        },
        error: function (e) {
            alert('访问出错！');
            return false;
        }
    });

    //随机10条
    $.ajax({
        url: api + "index/showWishs",
        data: {
            token: token,
        },
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data.status != 0) {
                alert(data.msg);
                return false;
            }
            var html = " <ul class='randoms'>";
            var list = data.data;
            for (var i = 0; i < list.length; i++) {
                html += " <li>" +
                    "                    <a class='cl thisContent subDianzan'  data-wishid='" + list[i].id + "'  style='cursor: pointer' title='点赞'>" +
                    "                        <span>" + list[i].wish_content + "</span>" +
                    "                        <span style='float: right' class='ddan' data-wishid='" + list[i].id + "'>点赞</span>" +
                    "                    </a>" +
                    "     </li>"
            }
            html += "</ul>";
            $(".randomlist").append(html);
            //点赞
            $(".subDianzan").click(function () {
                var wish_id = $(this).attr('data-wishid');
                $.ajax({
                    url: api + "index/givePraise",
                    data: {
                        token: token,
                        wish_id: wish_id
                    },
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        alert(data.msg);
                        if (data.status != 0) {
                            return false;
                        } else {
                            window.location.href = "index.html";
                        }
                    },
                    error: function (e) {
                        alert('访问出错！');
                        return false;
                    }
                });
            })
        },
        error: function (e) {
            alert('访问出错！');
            return false;
        }
    });

    //获取前十排行榜
    $.ajax({
        url: api + "index/wishesRanking",
        data: {
            token: token,
        },
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data.status != 0) {
                alert(data.msg);
                return false;
            }
            var html = " <ul class='rankinglist'>";
            var list = data.data.list;
            console.log(list);
            for (var i = 0; i < list.length; i++) {
                var num = i + 1;
                html += " <li>" +
                    "                    <a class='cl thisContent subDianzan'  data-wishid='" + list[i].id + "'  style='cursor: pointer' title='点赞'>" +
                    "                        <span>No." + num + "</span>" +
                    "                        <span>" + list[i].wish_content + "</span>" +
                    // "                        <span style='float: right' class='ddan' data-wishid='" + list[i].id + "'>点赞</span>" +
                    "                    </a>" +
                    "     </li>"
            }
            html += "</ul>";
            $(".ranking").append(html);
        },
        error: function (e) {
            alert('访问出错！');
            return false;
        }
    });

});

//发送祝福语
$(".btn_sub_wish").click(function () {
    var content = $(".wishDetail").val();
    $.ajax({
        url: api + "index/sendWishs",
        data: {
            token: token,
            content: content
        },
        type: "POST",
        dataType: "json",
        success: function (data) {
            if (data.status != 0) {
                alert(data.msg);
                return false;
            }
            alert('发送成功！');
            localStorage.setItem('can_send_wishes', 0);
            $(".subWishLine").attr("style", "display:none;");//隐藏div
            window.location.href = "index.html";
        },
        error: function (e) {
            alert('访问出错！');
            return false;
        }
    });
})

//领取cdk
$(".lingqu").click(function () {
    if (isLingQuCdk == 1) {
        alert("您已经领取过CDK啦!");
        return false;
    }
    $.ajax({
        url: api + "index/reveiveCDK",
        data: {
            token: token,
        },
        type: "GET",
        dataType: "json",
        success: function (data) {
            alert(data.msg);
            if (data.status != 0) {
                return false;
            }
            window.location.href = "index.html";
        },
        error: function (e) {
            alert('访问出错！');
            return false;
        }
    });
})






